package com.yuyou.fn.controller;


import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.electronicinvoice.entity.FinanceSubject;
import com.yuyou.fn.electronicinvoice.entity.Invoice;
import com.yuyou.fn.electronicinvoice.entity.RecordTempInvoice;
import com.yuyou.fn.electronicinvoice.service.IFinanceSubjectService;
import com.yuyou.fn.electronicinvoice.service.IInvoiceService;
import com.yuyou.fn.electronicinvoice.service.IRecordTempInvoiceService;
import com.yuyou.fn.electronicinvoice.vo.FinanceSubjectQueryVo;
import com.yuyou.fn.electronicinvoice.vo.FinanceSubjectSaveVo;
import com.yuyou.fn.electronicinvoice.vo.InvoiceSaveVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Pattern;
import java.rmi.RemoteException;
import java.util.List;

/**
 * Created by itachi on 2017/04/05.
 */
@Controller
@RequestMapping("/api/admin/electronicinvoice")
public class AdminElectronicInvoiceController extends ApiBaseController {

    @Resource
    private IFinanceSubjectService financeSubjectService;

    @Resource
    private IInvoiceService invoiceService;

    @Resource
    private IRecordTempInvoiceService recordTempInvoiceService;

    @ApiOperation("保存或者更新发票财务主体")
    @RequestMapping(value = "/saveOrUpdate" ,method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveOrUpdate(@ModelAttribute FinanceSubjectSaveVo financeSubjectSaveVo, HttpServletRequest request){
        financeSubjectService.saveOrUpdate(financeSubjectSaveVo);
        return ReturnResult.success("ok");
    }

    @ApiOperation("发票财务主体0停用，1启用")
    @RequestMapping(value = "/changeSubjectStatus" ,method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> changeSubjectStatus(@RequestParam("financeSubjectId")Long financeSubjectId,@RequestParam("subjectStatus")Integer subjectStatus, HttpServletRequest request){
        financeSubjectService.changeSubjectStatus(financeSubjectId,subjectStatus);
        return ReturnResult.success("ok");
    }



    @ApiOperation("发票财务主体列表")
    @RequestMapping(value = "/financeSubjectList" ,method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<FinanceSubject> > financeSubjectList(@ModelAttribute FinanceSubjectQueryVo financeSubjectQueryVo,@RequestParam(value = "pageNo")int pageNo,@RequestParam(value = "pageSize")int pageSize, HttpServletRequest request){

        Page<FinanceSubject> financeSubjectPage=financeSubjectService.findByQueryVo(financeSubjectQueryVo,pageNo,pageSize);
        return ReturnResult.success(financeSubjectPage);
    }

    @ApiOperation("单个发票财务主体")
    @RequestMapping(value = "/financeSubject" ,method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<FinanceSubject> financeSubject(@RequestParam(value = "financeSubjectId")Long financeSubjectId, HttpServletRequest request){
        FinanceSubject financeSubject=financeSubjectService.findById(financeSubjectId);
        return ReturnResult.success(financeSubject);
    }

    @ApiOperation("点击开票")
    @RequestMapping(value = "/openInvoice" ,method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<InvoiceSaveVo> openInvoice(@RequestParam(value = "orderId") long orderId, @Pattern(regexp = "[regId][courseOrderId]") @RequestParam(value = "orderType",defaultValue = "regId")String orderType, HttpServletRequest request){
        InvoiceSaveVo invoiceSaveVo=invoiceService.openInvoice(orderId,1,orderType);
        return ReturnResult.success(invoiceSaveVo);
    }





    @ApiOperation("点击红冲发票")
    @RequestMapping(value = "/rushInvoice" ,method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Invoice> rushInvoice(@RequestParam(value = "invoiceId") Long invoiceId,HttpServletRequest request){
        return ReturnResult.success(invoiceService.rushInvoice(invoiceId));
    }





    @ApiOperation("正常开票保存")
    @RequestMapping(value = "/saveInvoice" ,method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<InvoiceSaveVo> saveInvoice(@ModelAttribute InvoiceSaveVo invoiceSaveVo, HttpServletRequest request) throws org.apache.axis2.AxisFault,RemoteException{
        invoiceSaveVo.setInvoiceType(1);
        invoiceSaveVo.setOperationCode(10);
        Invoice invoice=invoiceService.saveOrRushInvoice(invoiceSaveVo);
        return ReturnResult.success(invoice);
    }

    @ApiOperation("红冲票保存")
    @RequestMapping(value = "/saveRushInvoice" ,method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Invoice> saveRushInvoice(@ModelAttribute InvoiceSaveVo invoiceSaveVo, HttpServletRequest request)throws org.apache.axis2.AxisFault,RemoteException {
        invoiceSaveVo.setInvoiceType(2);
        invoiceSaveVo.setOperationCode(21);
        Invoice invoice=invoiceService.saveOrRushInvoice(invoiceSaveVo);
        return ReturnResult.success(invoice);
    }



    @ApiOperation("发票列表")
    @RequestMapping(value = "/invoiceList" ,method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<Invoice>> invoiceList(@RequestParam("orderId")long orderId, HttpServletRequest request)throws org.apache.axis2.AxisFault,RemoteException {
        List<Invoice> invoiceList=invoiceService.findInvoiceByOrderId(orderId);
        return ReturnResult.success(invoiceList);
    }

    @ApiOperation("重发邮件")
    @RequestMapping(value = "/resendMail" ,method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> resendMail(@ModelAttribute InvoiceSaveVo invoiceSaveVo, HttpServletRequest request)throws org.apache.axis2.AxisFault,RemoteException {

        invoiceService.changeAndSendEmail(invoiceSaveVo);
        return ReturnResult.success("ok");
    }

    @ApiOperation("发票详情")
    @RequestMapping(value = "/invoiceDetail" ,method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Invoice> invoiceDetail(@RequestParam("invoiceId")Long invoiceId, HttpServletRequest request)throws org.apache.axis2.AxisFault,RemoteException {
        Invoice invoice=invoiceService.findById(invoiceId);
        return ReturnResult.success(invoice);
    }



}
